package com.example.xianji.activity;

import org.json.JSONException;
import org.json.JSONObject;

import com.example.xianji.Contant;
import com.example.xianji.R;
import com.example.xianji.constant.Constant;
import com.example.xianji.utils.sp.PreferenceEngine;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.os.Bundle;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class Main_FuWu_LiuYanActivity extends Activity implements OnClickListener {
    private ImageView fuwu_liuyan_back;
    //    private SharedPreferences sp;
//    private Editor ed;
    private Button post_liuyan;
    private EditText edittext_liuyan_details;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        setContentView(R.layout.activity_main__fu_wu__liu_yan);
        //XJApplication.getInstance().addActivity(this);
        fuwu_liuyan_back = (ImageView) findViewById(R.id.fuwu_liuyan_back);
//        sp = getSharedPreferences("text", MODE_PRIVATE);
//        ed = sp.edit();
        post_liuyan = (Button) findViewById(R.id.post_liuyan);
        edittext_liuyan_details = (EditText) findViewById(R.id.edittext_liuyan_details);

        fuwu_liuyan_back.setOnClickListener(this);
        post_liuyan.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.fuwu_liuyan_back) {
            this.finish();
        } else if (v.getId() == R.id.post_liuyan) {
            if (PreferenceEngine.getInstance().getBoolean(Constant.SP.NAME, Constant.SP.KEY_XIANJI_DENGLU, false)) {
//            if (sp.getBoolean("xianji_denglu", false)) {
                if (edittext_liuyan_details.getText().toString().length() > 0) {
                    post_liuyan();
                } else {
                    Toast.makeText(Main_FuWu_LiuYanActivity.this, "请填写留言内容", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(Main_FuWu_LiuYanActivity.this, "请先登录", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(Main_FuWu_LiuYanActivity.this, Main_LoginActivity.class));
            }
        }
    }

    public void post_liuyan() {
        HttpUtils httpUtils = new HttpUtils();
        RequestParams parms = new RequestParams();
        parms.addBodyParameter("token", PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TOKEN, ""));
//        parms.addBodyParameter("token", sp.getString("xianji_token", ""));
        parms.addBodyParameter("details", edittext_liuyan_details.getText().toString());
        if (getIntent().getIntExtra("liuyan_comefrom", 0) == 1) {
            //从服务详情传过来的，给服务留言
            parms.addBodyParameter("case_id", getIntent().getIntExtra("fuwu_id", 0) + "");
        } else if (getIntent().getIntExtra("liuyan_comefrom", 0) == 2) {
            //从产品详情传过来的，给公司留言
            parms.addBodyParameter("product_id", getIntent().getStringExtra("fuwu_id"));
        }


        httpUtils.send(HttpMethod.POST, Contant.fuwu_liuyan, parms, new RequestCallBack<String>() {

            @Override
            public void onFailure(HttpException arg0, String arg1) {
                Log.i("留言留言留言留言留言留言", arg1);
            }

            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                try {
                    JSONObject object = new JSONObject(arg0.result);
                    Toast.makeText(Main_FuWu_LiuYanActivity.this, object.getString("msg"), Toast.LENGTH_SHORT).show();
                    if (object.getInt("status") == 1) {
                        Main_FuWu_LiuYanActivity.this.finish();
                    }
                } catch (JSONException e1) {
                    e1.printStackTrace();
                }
            }
        });
    }
}
